﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using System.Linq;
using Pyramid.VsOnline.Core.Services;
using Pyramid.VsOnline.Core.Model;

namespace Pyramid.VsOnline.TfsGet
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length > 0 && args[0].ToLower() == "-h")
            {
                DisplayHelp();
                return;
            }
            var tfsManager = new TfsManager();
            tfsManager.OnNonFatalError += Program.OnNonFatalError;
            var parameterParser = new TfsParamParser();
            try
            {
                var parameters = parameterParser.Parse(args);
                if (parameters.IsWorkspaces)
                {
                    var result = tfsManager.GetWorkspaces(parameters);
                    Console.Write(result);
                }
                if(parameters.IsCreateWorkspace)
                {
                    var result = tfsManager.CreateWorkspace(parameters);
                    Console.Write(result);
                }
                if (parameters.IsWorkfold)
                {
                    var result = tfsManager.MapWorkspace(parameters);
                    Console.Write(result);
                    tfsManager.DownloadLatestFiles(parameters);
                }
                if (parameters.IsGetLatestVersion)
                {
                    //TODO: serverUrl missing from hudson
                    //VerifyServerUrl(parameters);
                    //tfsManager.GetLatestVersion(parameters);
                }
                if (parameters.IsGetHistory)
                {
                    var result = tfsManager.GetHistory(parameters);
                    Console.WriteLine(result);
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
                DisplayHelp();
                Environment.Exit(1);
            }
        }
        internal static void DisplayHelp()
        {
            string helpFormatText = "workspaces [-format:{brief/detailed}] -server:{collectionUrl} -login:{username,password}\n\n";
            Console.WriteLine(helpFormatText);

            helpFormatText = "workspace -new {workspaceName};{owner} [-noprompt] -server:{collectionUrl} -login:{username,password}\n\n";
            Console.WriteLine(helpFormatText);

            helpFormatText = "workfold -map {serverPath} {localPath} -workspace:{workspaceName} -server:{collectionUrl} -login:{username,password}\n\n";
            Console.WriteLine(helpFormatText);

            helpFormatText = "get {localPath} [-recursive] -version:{fromDate} [-noprompt] -login:{username,password}\n\n";
            Console.WriteLine(helpFormatText);

            helpFormatText = "history {serverPath} [-noprompt] -version:fromDate~toDate [-recursive] [-format:{brief/detailed}] -server:{collectionUrl} -login:{username,password}\n\n";
            Console.WriteLine(helpFormatText);
            Console.WriteLine("Accepted date format is: Dyyyy-MM-ddTHH:mm:ssZ");
        }
        internal static void VerifyServerUrl(TfsParameters tfsParameters)
        {
            var serverUrl = tfsParameters.ServerUrl;
            if(string.IsNullOrWhiteSpace(serverUrl))
            {
                if(tfsParameters.IsGetLatestVersion)
                {
                    tfsParameters.ServerUrl = GetSavedServerUrl(tfsParameters.LatestVersion.TargetPath);
                }
            }
        }
        internal static string GetSavedServerUrl(string localMappedPath)
        {
            Dictionary<string,string> mappedLocalPathAndServerUrl = new Dictionary<string, string>();
            mappedLocalPathAndServerUrl.Add(@"C:\Users\irabantac\.hudson\jobs\Pyramid MVC5 Template\workspace".ToLower(), "https://irabanta.visualstudio.com/DefaultCollection");

            return mappedLocalPathAndServerUrl[localMappedPath.ToLower()];
        }
        internal static void OnNonFatalError(Object sender, ExceptionEventArgs e)
        {
            var message = e.Exception != null ? e.Exception.Message : e.Failure.Message;
            Console.Error.WriteLine("Exception: " + message);
        }
    }
}
